package edu.chalmers.AV_2DPlatformerFramework.Model;

/**
 * The thread responsible for updating the game.
 * @version 0.01
 * @author Richard
 */
public class GameThread extends Thread {
    private int delay;
    private final Game game;
    private int ticks = 0; //resets after ~25 days with tickrate 1 update/ms.
    
    public GameThread(Game game, int delay){
        this.delay = delay;
        this.game = game;
        this.start();
    }
    
    public GameThread(Game game){
        this(game, 10); //defaults to 10 ms
    }
    
    @Override
    public void run() {
        while(true){
            try{
                game.run();
                ticks++;
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                System.exit(1);
            }
        }
    }
    
    public int detTickNo(){
        return ticks;
    }
    public int getDelay(){
        return this.delay;
    }
    public void setDelay(int delay){
        if (delay > 0)
            this.delay = delay;
    }
    public void setUpdateFrequenzy(int hz){
        if (hz > 0)
            this.delay = hz/1000;
    }
}
